$(function () {
    $("#jqGrid").Grid({
        url: '../topic/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '素材类别id', name: 'topicCategoryId', index: 'topic_category_id', hidden: true},
            {label: '素材类别', name: 'topicCategoryTitle', index: 'topic_category_title', width: 40},
            {label: '素材名称', name: 'content', index: 'content', width: 100},
            {
                label: '素材图片', name: 'itemPicUrl', index: 'item_pic_url', width: 80, formatter: function (value) {
                    if (value) {
                        var valArr = value.split(",");
                        var imgs = "";
                        for(var i = 0 ; i < valArr.length; i++){
                            imgs += '<img width="50px" height="50px" src="' + valArr[i] + '">';
                        }
                        return imgs;
                    } else {
                        return '-';
                    }
                }
            }
            ]
    });
    $('#content').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: 'auto', //高度
        language: "zh_cn",
        minHeight: '200px',
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',//上传到本地服务器
        imageUploadParams: {id: "edit"},
        // imageManagerDeleteURL: '../sys/oss/delete',//删除图片(有问题)
        imagesLoadURL: '../sys/oss/queryAll'//管理图片
    });

    $('#avatar').editable({
        inlineMode: false,
        alwaysBlank: true,
        height: 'auto', //高度
        language: "zh_cn",
        minHeight: '200px',
        spellcheck: false,
        plainPaste: true,
        enableScript: false,
        imageButtons: ["floatImageLeft", "floatImageNone", "floatImageRight", "linkImage", "replaceImage", "removeImage"],
        allowedImageTypes: ["jpeg", "jpg", "png", "gif"],
        imageUploadURL: '../sys/oss/upload',//上传到本地服务器
        imageUploadParams: {id: "edit"},
        // imageManagerDeleteURL: '../sys/oss/delete',//删除图片(有问题)
        imagesLoadURL: '../sys/oss/queryAll'//管理图片
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        topic: {topicCategoryId: ''},
        ruleValidate: {
        },
        topicCategorys: [],
        q: {
            content: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.topic = {topicCategoryId: ''};
            $('#content').editable('setHTML', '');
            $('#avatar').editable('setHTML', '');
            vm.getTopicCategorys();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);

            vm.getTopicCategorys();
        },

        getTopicCategorys: function () {
            Ajax.request({
                url: "../topiccategory/queryAll",
                async: true,
                successCallback: function (r) {
                    vm.topicCategorys = r.list;
                }
            });
        },

        saveOrUpdate: function (event) {
            var url = vm.topic.id == null ? "../topic/save" : "../topic/update";

            if(vm.topic.topicCategoryId == null || vm.topic.topicCategoryId == ''){
                alert('请先选择素材类别！');
                return false;
            }
            //编辑器内容
            vm.topic.content = $('#content').editable('getHTML');
            vm.topic.avatar = $('#avatar').editable('getHTML');

            Ajax.request({
                type: "POST",
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.topic),
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    type: "POST",
                    url: "../topic/delete",
                    contentType: "application/json",
                    params: JSON.stringify(ids),
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });


            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../topic/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.topic = r.topic;
                    $('#content').editable('setHTML', vm.topic.content);
                    $('#avatar').editable('setHTML', vm.topic.avatar);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'content': vm.q.content},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },

        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});